//
//  DelegateViewController.m
//  BooksViewTable
//
//  Created by mac on 15-12-10.
//  Copyright (c) 2015年 yb. All rights reserved.
//

#import "DelegateViewController.h"
#import "ViewController.h"
<<<<<<< HEAD
#import "readViewController.h"
@interface DelegateViewController ()<UIPageViewControllerDataSource, UIPageViewControllerDelegate>
{
    NSUInteger currentpage;
    NSUInteger allpage;
    UITextView *textView;
    UIBarButtonItem *btn4;
    UIPageViewController *pageViewCon;
    UIToolbar *toolbar;
    CGFloat totalHeight;
    NSUInteger totalPages;

}
=======
@interface DelegateViewController ()

>>>>>>> 475f81e3bc27cc7832b67614584a5dc39e5e8d10
@end

@implementation DelegateViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view.
<<<<<<< HEAD
    //按钮
    [UIView beginAnimations:nil context:nil];
    [UIView commitAnimations];
    //背景
    toolbar  = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, 320, 44)];
    toolbar.tintColor = [UIColor grayColor];
    [self.view addSubview:toolbar];
    //翻页
    pageViewCon = [[UIPageViewController alloc] initWithTransitionStyle:UIPageViewControllerTransitionStylePageCurl navigationOrientation:UIPageViewControllerNavigationOrientationHorizontal options:@{UIPageViewControllerOptionSpineLocationKey : @(UIPageViewControllerSpineLocationNone)}];
    //协议等于self
    pageViewCon.dataSource = self;
    pageViewCon.delegate = self;
    [self addChildViewController:pageViewCon];
    //位置
    pageViewCon.view.frame = CGRectMake(0, 64, self.view.frame.size.width, self.view.frame.size.height - 64 - 44);
    
    //pageViewCon.view.transform = CGAffineTransformMakeRotation(M_2_PI);
    
    //CGAffineTransform transform = CGAffineTransformMakeRotation(M_2_PI);
    //transform = CGAffineTransformScale(transform, 0.8, 0.8 );
    //pageViewCon.view.transform = transform;
    [self.view addSubview:pageViewCon.view];
    
    toolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, self.view.frame.size.height - 44, self.view.frame.size.width, 44)];
    [self.view addSubview:toolbar];
    
    CGRect rect = [self.text boundingRectWithSize:CGSizeMake(self.view.frame.size.width, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[[UILabel new].font fontWithSize:24.0]} context:nil];
    totalHeight = ceil(rect.size.height);
    totalPages = ceil(totalHeight / (self.view.frame.size.height - 64 - 44));
    //每一页的文字多少
    allpage = _text.length / totalPages;
    currentpage = 0;
    //按钮
    UIBarButtonItem *btn1 = [[UIBarButtonItem alloc]initWithTitle:@"我的书架" style:UIBarButtonItemStylePlain target:self action:@selector(onClick)];
    UIBarButtonItem *btn2 = [[UIBarButtonItem alloc]initWithTitle:@"上一页" style:UIBarButtonItemStylePlain target:self action:@selector(upPage)];
    UIBarButtonItem *btn3 = [[UIBarButtonItem alloc]initWithTitle:@"下一页" style:UIBarButtonItemStylePlain target:self action:@selector(downPage)];
    //btn4 = [[UIBarButtonItem alloc]initWithTitle:[NSString stringWithFormat:@"%lu/%lu",currentpage +1,allpage] style:UIBarButtonItemStylePlain target:self action:nil];
    
     //NSArray * array = @[btn1,btn2,btn3,btn4];
    //NSArray *array = [[NSArray alloc]initWithObjects:btn1,btn2,btn3,btn4,nil];
    //[toolbar setItems:array animated:YES];
    
    //书页码的现实
    btn4 = [[UIBarButtonItem alloc] initWithTitle:[NSString stringWithFormat:@"%lu／%lu",currentpage + 1, totalPages] style:UIBarButtonItemStylePlain target:nil action:nil];
    toolbar.items = @[btn1,btn2,btn3,btn4];
    
    //手势
    UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(pan:)];
    [textView addGestureRecognizer:pan];
    //引进另一个VIEWContrller
    readViewController *con = [readViewController new];
    con.text = [_text substringWithRange:NSMakeRange(currentpage * allpage, allpage)];
    [pageViewCon setViewControllers:@[con] direction:UIPageViewControllerNavigationDirectionForward animated:NO completion:nil];
    
}

-  (void)pan:(UIPanGestureRecognizer *)pan
{
    //手势的实现
    CGPoint point = [pan translationInView:textView];
    if (point.x < -5 && point.x >10) {
        [pan setTranslation:CGPointMake(0, 0) inView:textView];
        [self uppage];
    }else if (point.x > 5 && point.x < 10) {
        [self downpage];
        [pan setTranslation:CGPointMake(0, 0) inView:textView];
        return;
    }
    [pan setTranslation:CGPointMake(0, 0) inView:textView];
    NSLog(@"%f",point.x);
    return;
}

- (void)uppage
{
    //翻页按钮上一页
    if (currentpage == 1) {
        UIAlertView *alertview = [[UIAlertView alloc]initWithTitle:@"温馨提示" message:@"这已经是第一页" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil];
        [alertview show];
        return;
    }
    currentpage = currentpage -1;
    NSLog(@"%lu",currentpage);
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:0.1];
    [textView setContentOffset:CGPointMake(0, (currentpage - 1) * 430) animated:YES];
    [UIView commitAnimations];
    
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:1];
    [UIView setAnimationTransition:UIViewAnimationTransitionCurlDown forView:self.view cache:YES];
    
    [UIView commitAnimations];
    
    [btn4 setTitle:[NSString stringWithFormat:@"%lu/%lu",currentpage,allpage]];
    return;
    
}

- (void)downpage
{
    //翻页按钮下一页
    if (currentpage == allpage) {
        UIAlertView *alertview = [[UIAlertView alloc]initWithTitle: @"温馨提示"message:@"这已经是最后一页" delegate: self cancelButtonTitle:@"确定" otherButtonTitles: nil];
        [alertview show];
        return;
    }
    currentpage = currentpage +1;
    NSLog(@"%lu",currentpage);
    [btn4 setTitle:[NSString stringWithFormat:@"%lu/%lu",currentpage,allpage]];
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:0.1];
    [textView setContentOffset:CGPointMake(0, (currentpage - 1) * 430) animated:YES];
    [UIView commitAnimations];
    
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:1];
    [UIView setAnimationTransition:UIViewAnimationTransitionCurlUp forView:self.view cache:YES];
    [UIView commitAnimations];
    readViewController *con = [readViewController new];
    con.text = [_text substringWithRange:NSMakeRange(currentpage * allpage , allpage)];
    [pageViewCon setViewControllers:@[con] direction:UIPageViewControllerNavigationDirectionForward animated:NO completion:nil];
    return;
}

- (void)refreshBarItemShow
{
    //定义方法，存放书页码
    btn4.title = [NSString stringWithFormat:@"第%lu页／共%lu页",currentpage + 1, totalPages];
}

- (UIViewController *)getCurContentController
{
    //定义方法
    readViewController *con = [readViewController new];
    con.text = [_text substringWithRange:NSMakeRange(currentpage * allpage, allpage)];
    con.index = currentpage;
    return con;
}

- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)viewController
{
    //翻页手指触摸翻页上一页
    if (currentpage <= 0) {
        return nil;
    }
    currentpage--;
    [self refreshBarItemShow];
    return [self getCurContentController];
}

- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController
{
    //翻页手指触摸翻页下一页
    if (currentpage >= totalPages - 1) {
        return nil;
    }
    currentpage++;
    [self refreshBarItemShow];
    return [self getCurContentController];
}



- (void)onClick
{
    [self dismissModalViewControllerAnimated:YES];
    return;
}

=======
    [UIView beginAnimations:nil context:nil];
    
    [UIView commitAnimations];
    
    UIToolbar *toolbar  = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, 320, 44)];
    toolbar.tintColor = [UIColor grayColor];
    [self.view addSubview:toolbar];
    
    
    //UIBarButtonItem *btn1 = [[UIBarButtonItem alloc]initWithTitle:@"我的书架" style:UIBarButtonItemSty target:self action:@selector(onClick)];
    
    
}

>>>>>>> 475f81e3bc27cc7832b67614584a5dc39e5e8d10
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
